// myCourse_package/pages/myCourse/myCourse.ts
import {getMyCourseList} from '../../../api/myCourse'
import {getMemberList} from '../../../api/member'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    screenHeight:0,
    myCourseList:[], //我的课程列表
    formData:{
      pageNum:1,
      pageSize:10,  
    },
    total:0, //总页数
    moreLimt:false,//是否加载完成
    isfreshComplete:false, //是否刷新完成
    tabsList:[]
  },
    // 列表刷新课程
    loadPageEvent(data:any){
      const type = data.detail.type
      switch(type){
        case 'refresh':
          this.resetPage()
          this.getMyCourseList()
          this.setData({
            isfreshComplete:true
          })
          break;
         case 'load':
          let formData = this.data.formData as any
          if(formData.pageNum * formData.pageSize < this.data.total){
            formData.pageNum  = formData.pageNum + 1
            this.setData({
              formData
            })
            this.getMyCourseList()
          }else{
           this.setData({
            moreLimt: true
           })
          }
          break;
      }
      
    
      },
        // 重置页码
  resetPage(){
    let formData = this.data.formData as any
    formData.pageNum = 1
   this.setData({
    formData,
    myCourseList:[]
   })
  },
  //切换筛选条件
  handleChange(e:any){
  let stuId:number = e.detail
  if(stuId === 0){
    this.setData({
      formData:{
        pageNum:1,
        pageSize:10,  
      },
      myCourseList:[]
    })
  }else{
    const newForm = {...this.data.formData,stuId}
    this.setData({
      formData:newForm,
      myCourseList:[]
    })
  }

  this.getMyCourseList()
  },
  //前去课程介绍页面
  courseDetail(event:any){
    const {title,stuid,serialid} =event.currentTarget.dataset
   wx.navigateTo({
     url:`/myCourse_package/pages/CourseIntroduction/CourseIntroduction?title=${title}&stuId=${stuid}&serialId=${serialid}`
   })
  },
  // 查看历史课程
  toSeeHistory(){
    wx.navigateTo({
      url:'/myCourse_package/pages/historyCourse/historyCourse'
    })
  },
  // 获取课程列表
  async getMyCourseList(){
   const {code, data:res} = await getMyCourseList(this.data.formData)
   if(code === 200){
    const {list,total} = res;
   let myCourseList = this.data.myCourseList.concat(list) 
     this.setData({
      myCourseList,
      total
     })
   }
  },
  // 获取我名下学员列表
 async getMyMember(){
   const  {userId} = getApp().getUserInfo()
  const {data:res} = await getMemberList({userId})
  const list = res.map((val:any)=>{
    return {
      id:val.stuId,
      label:val.userName
    }
  })
  
  list.unshift({
    id:0,
    label:'全部'
  })
  this.setData({
    tabsList:list
  })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(data:any) {
    wx.getSystemInfo({
      success: (res) => {
        this.setData({
          screenHeight: res.windowHeight
        });
      }
    });
    // 获取我的课程列表
    this.getMyCourseList()
    // 获取学员列表
    this.getMyMember()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})